package com.dayaokeji.rhythmschoolstudent.client.home.utils


import com.dayaokeji.rhythmschoolstudent.utils.DateUtils
import com.dayaokeji.server_api.domain.Course
import com.dayaokeji.server_api.domain.Meeting
import java.util.*

/**
 *********************************************************************
 **  Created by Zeno on 2018/9/14
 *********************************************************************
 **  version 1.0
 *********************************************************************
 **  Copyright (C) dayaokeji.com 2018 -- 2020 All rights reserved.
 *********************************************************************
 ** Home 最新会议课程时间过滤
 *  如果当前时间在某课程会议之间，则显示此课程会议
 *  如果今天课上完了，则显示明天的课程
 *********************************************************************
 */
object HomeNewTopTimeFilterHelper {

    private val calendar: Calendar by lazy { Calendar.getInstance() }

    private fun getCurrentDate(): Date {
        calendar.timeInMillis = System.currentTimeMillis()
        calendar.add(Calendar.MINUTE,10) // 提前十分钟
        return calendar.time
    }


    fun courseTimeFilter(courses: List<Course>): Course?{
        for (cours in courses) {
            val actStartTime = DateUtils.formatDate(cours.actStartTime, DateUtils.FORMAT_DATETIME)
            val actEndTime = DateUtils.formatDate(cours.actEndTime, DateUtils.FORMAT_DATETIME)
            if (getCurrentDate() == actEndTime || getCurrentDate() == actStartTime) {
                return cours
            }else if (getCurrentDate().after(actStartTime) && getCurrentDate().before(actEndTime)) {
                return cours
            }else if (getCurrentDate().before(actStartTime)){
                return cours
            }
        }
        return if (courses.isNotEmpty()) courses[0] else null
    }


    fun meetingTimeFilter(meetings: List<Meeting>): Meeting? {
        for (meeting in meetings) {
            val actStartTime = DateUtils.formatDate(meeting.actStartTime, DateUtils.FORMAT_DATETIME)
            val actEndTime = DateUtils.formatDate(meeting.actEndTime, DateUtils.FORMAT_DATETIME)
            if (getCurrentDate() == actStartTime || getCurrentDate() == actEndTime) {
                return meeting
            }else if (getCurrentDate().after(actStartTime) && getCurrentDate().before(actEndTime)) {
                return meeting
            }else if (getCurrentDate().before(actStartTime)) {
                return meeting
            }
        }
        return if (meetings.isNotEmpty()) meetings[0] else null
    }

}